package com.craftcomputing.client.Entry;

import com.craftcomputing.client.Listeners.EntryPanelListeners;
import com.craftcomputing.client.Listeners.SearchResultLabelListeners.ImageLoadListener;
import com.craftcomputing.client.Listeners.SearchResultListeners.JSONLinkedEntryResponseTextHandler;
import com.craftcomputing.client.SearchResult.WorkshopSearchResult;
import com.craftcomputing.client.Entry.PhotoWidget.*;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.*;

public class ThemeEntryPanel extends Composite {

	public ThemeEntryPanel() {
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		panel.setStyleName("entryPanel");

		final VerticalPanel verticalPanel_4 = new VerticalPanel();
		panel.add(verticalPanel_4);
		panel.setCellVerticalAlignment(verticalPanel_4, HasVerticalAlignment.ALIGN_TOP);
		verticalPanel_4.setWidth("100%");	

		panel.add(linkedWorkshopsPanel);
		linkedWorkshopsPanel.setStyleName("gwt-paddedBottom");
		linkedWorkshopsPanel.setWidth("800px");
		panel.setCellVerticalAlignment(linkedWorkshopsPanel, HasVerticalAlignment.ALIGN_TOP);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		linkedWorkshopsPanel.add(horizontalPanel);
		horizontalPanel.setStyleName("gwt-SingleEntryTab");
		horizontalPanel.setSpacing(4);
		horizontalPanel.setWidth("100%");

		horizontalPanel.add(workshopsUsedInLabel);
		workshopsUsedInLabel.setWidth("100%");
		workshopsUsedInLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		linkedWorkshopsPanel.add(workshopsPanel);
		workshopsPanel.setSize("100%", "100%");
		workshopsPanel.setStyleName("gwt-TabPanel-SingleResult");

		workshopSearchResult.setStyleName("gwt-TabBar .gwt-Result");
		workshopsPanel.add(workshopSearchResult);
		workshopSearchResult.setSize("100%", "100%");

		verticalPanel_4.add(horizontalPanel_4);
		horizontalPanel_4.setWidth("100%");
		horizontalPanel_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel_4.add(photo);
		horizontalPanel_4.setCellHorizontalAlignment(photo, HasHorizontalAlignment.ALIGN_LEFT);

		horizontalPanel_4.add(richTextArea);
		richTextArea.setWidth("455px");
		richTextArea.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		horizontalPanel_4.setCellVerticalAlignment(richTextArea, HasVerticalAlignment.ALIGN_MIDDLE);
		richTextArea.setStyleName("gwt-RichTextArea");
		horizontalPanel_4.setCellHorizontalAlignment(richTextArea, HasHorizontalAlignment.ALIGN_LEFT);

	}
	public void update(JSONValue json){
    	final String workshopName = json.isObject().get("name").toString().replaceAll("\"", "");
    	final String imageLoc = json.isObject().get("image").toString().replaceAll("\"", "");
		titleLabel.setText(workshopName);
		horizontalPanel_4.remove(photo);
		photo = new PhotoWidget(imageLoc);
		horizontalPanel_4.insert(photo, 0);

		if(json.isObject().get("workshops") != null){
			JSONArray workshops = json.isObject().get("workshops").isArray();
			if(workshops != null && workshops.size() > 0)
				workshopSearchResult.setLabels(workshops, "");
			else {
				linkedWorkshopsPanel.remove(workshopsPanel);
				workshopsUsedInLabel.setText("No Workshops use this Theme");
			}
		}
		else{
			final String workshopIds = json.isObject().get("workshopIds").toString();
			//Window.alert("WorkshopIds = " + workshopIds);
			String searchText = "/python/workshops/fromtheme?workshopIds=" + workshopIds;
			HTTPRequest.asyncGet(searchText, new JSONLinkedEntryResponseTextHandler(workshopSearchResult));
		}
	}
	private VerticalPanel workshopsPanel = new VerticalPanel();
	private Label workshopsUsedInLabel = new Label("Workshops that use this Theme");
	private VerticalPanel linkedWorkshopsPanel = new VerticalPanel();
	private PhotoWidget photo  = new PhotoWidget("lightbox/close.gif");	
	private Label titleLabel = new Label("Theme Title");
	private Label richTextArea = new Label("More info coming soon.");
	private WorkshopSearchResult workshopSearchResult = new WorkshopSearchResult(false);
	private HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
	public String getTitle(){
		return titleLabel.getText();
	}
}
